<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <div style="margin-bottom: 20px;">
      <el-button type="primary" size="medium"
                 @click="$router.push('/permission/role/add-new')">添加角色
      </el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="角色名称" width="180" align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="描述" header-align="center"
                       show-overflow-tooltip></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100" align="center"></el-table-column>

      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit" circle
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="text-align: right; margin: 10px auto;">
      <el-pagination
          @current-change="changePage"
          layout="total, prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize">
      </el-pagination>
    </div>

    <el-dialog title="修改角色" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" :rules="rules" ref="ruleForm" label-width="100px">
        <div style="width: 30%;">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <el-form-item label="描述" prop="description">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <div style="width: 28%;">
        <el-form-item label="排序序号" prop="sort">
          <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
        </div>
        <el-form-item label="权限" prop="permissionIds">
          <el-select v-model="editForm.permissionIds" multiple placeholder="请选择">
            <el-option
                v-for="item in permissionListOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      permissionListOptions:[],
      tableData: [],
      total: 0,
      pageSize: 0,
      currentPage: 1,
      dialogFormVisible: false,
      editForm: {
        id: '',
        name: '',
        description: '',
        sort: '',
        permissionIds:[]
      },
      rules: {
        name: [
          {required: true, message: '请输入角色名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入描述', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ],
        permissionIds: [
          {required: true, message: '请选择权限', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    loadPermissionList() {
      let url = 'http://localhost:9201/permission/list?query=all';
      console.log('url = ' + url);


      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000) {
          this.permissionListOptions = jsonResult.data.list;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openEditDialog(role) {
      this.loadPermissionList();
      let url = 'http://localhost:9201/role/standard?id=' + role.id;
      console.log('url = ' + url);

      let localJwt = localStorage.getItem('localJwt');
      this.axios
          .create({
            'headers': {
              'Authorization':localJwt
            }
          }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadRoleList();
            }
          });
        }
      });
    },
    submitEditForm() {
      if (this.editForm.id == 1) {
        if (this.editForm.permissionIds.length != this.permissionListOptions.length) {
          this.$message({
            message: '系统管理员必须拥有所有权限！',
            type: 'success'
          });
          return;
        }
      }


      let url = 'http://localhost:9201/role/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);
      let localJwt = localStorage.getItem('localJwt');

      this.axios
          .create({
        'headers': {
          'Authorization':localJwt
        }
      }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改角色成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadRoleList();
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    openDeleteConfirm(role) {
      if (role.id == 1) {
        this.$message({
          message: '不能删除系统管理员角色！',
          type: 'success'
        });
        return;
      }
      this.$confirm('此操作将永久删除【' + role.id + '-' + role.name + '】角色，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(role);
      }).catch(() => {
      });
    },
    handleDelete(role) {
      let url = 'http://localhost:9201/role/delete';
      console.log('url = ' + url);

      let formData = 'id=' + role.id;
      console.log('formData = ' + formData);

      let localJwt = localStorage.getItem('localJwt');
      this.axios
          .create({
            'headers': {
              'Authorization':localJwt
            }
          }).post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除角色成功！',
            type: 'success'
          });
          this.loadRoleList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadRoleList();
            }
          });
        } else if (jsonResult.state == 40900){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    changePage(value) {
      this.$router.replace('?page=' + value);
      this.loadRoleList();
    },
    loadRoleList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = 'http://localhost:9201/role/list?page=' + page;
      console.log('url = ' + url);
      let localJwt = localStorage.getItem('localJwt');

      this.axios
          .create({
            'headers': {
              'Authorization':localJwt
            }
          }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          this.total = jsonResult.data.total;
          this.pageSize = jsonResult.data.pageSize;
          this.currentPage = jsonResult.data.currentPage;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadRoleList();
  }
}
</script>